import '../assets/loading.css'
import '../assets/show_info.scss'
import dataJson from './data.json'
import { certImageGenerator } from '../frontEndCertImageGenerator.ts'


document.getElementById("loadingWrap")?.setAttribute('style', "display:none")

function setText(id: string, text: string): void {
    console.log(id, text)
    document.getElementById(id).innerText = text
}

// ================ 进行 windows.hash 检查
if (window.location.hash.length > 0) {
    let uuid = window.location.hash
    document.getElementById("main")?.setAttribute("style", "display:none")
    document.getElementById("show_info")?.setAttribute("style", "display")

    //check uuid
    let flag = 0
    let hash = window.location.hash.slice(1)
    for (let key in dataJson) {
        for (let item of dataJson[key]) {
            if (item["uuid"] == hash) {
                flag = 1
                console.log(item)
                setText("name", item["姓名"])
                setText("school", item["学校名称"] || item["学校"])
                setText("subject", key)
                setText("works_name", item["作品名称"] || item['作吕名'])
                setText("level", item["奖项"])
            }
        }
    }
    if (flag == 0) {
        alert("没有找到")
    }
}
// ===================== hash End

var imgBase = {}
const validation_url = "http://cert.xxjyxiehui.com/202203.html#"
if (process.env.NODE_ENV !== 'production') {
    // Only runs in development and will be stripped in production builds.
    imgBase = {
        "科幻画": ["http://127.0.0.1:8080/绘画", ".jpg"],
        "科幻小说": ["http://127.0.0.1:8080/小说", ".jpg"],
        "科学童话": ["http://127.0.0.1:8080/童话", ".jpg"],
    }
}
else {

    imgBase = {
        "科幻画": ["绘画", ".jpg"],
        "科幻小说": ["小说", ".jpg"],
        "科学童话": ["童话", ".jpg"],
    }
}

function get_imgBackUrl(catg: string, level: string): string {
    return imgBase[catg][0] + level[0] + imgBase[catg][1];
}


// ===================== 查询
function search_user(): void {

    //得到 分类
    let selectObj: HTMLSelectElement = (document.getElementById("catlog") as HTMLSelectElement)
    let idx = selectObj.selectedIndex
    let catlog = selectObj.options[idx].value
    console.log("catlog : ",catlog)

    //idnumber
    let idnumber = (document.getElementById("idnumber") as HTMLInputElement).value.toLowerCase()
    if (idnumber.length == 0) {
        alert("请输入 身份证!")
        return;
    }

    let loadingDom = document.getElementById("loadingWrap")
    loadingDom.setAttribute('style', "display:")
    let find_flag = 0
    for (let item of dataJson[catlog]) {
        if (item['身份证号'].toLowerCase() === idnumber) {
            console.log(item)
            find_flag = 1
            //let now_url: string = window.location.href
            //let t = now_url.split('/')
            //t.splice(-1)
            //let imgBackUrl = t.join('/')
            //if (imgBackUrl[imgBackUrl.length - 1] != '/')
            //    imgBackUrl += '/';
            //imgBackUrl += '绘画二.jpg'
            let imgBackUrl = get_imgBackUrl(catlog, item["奖项"]);
            console.log(imgBackUrl)
            let cert = certImageGenerator.createCanvas(document.getElementsByTagName("body")[0], 1984, 2806);
            cert.setBackground(imgBackUrl)
                .then(
                    () => {
                        loadingDom.setAttribute('style', "display:none")

                        document.getElementById("main").setAttribute("style", "display:none")

                        cert.setfont('simhei', 60)
                        if (item["姓名"].length >= 8 )
                            cert.setfont('simhei', 50)
                        let pos_x : number = (item["姓名"].length-2) * -30 + 450
                        cert.drawText(item["姓名"], pos_x, 1300); //姓名
                        cert.setfont('simhei', 50)
                        cert.drawText(item["作品名称"] || item["作品名"], 780, 1905);
                        cert.drawText(item["指导老师"], 780, 2065);
                        cert.qrcode(validation_url + item["uuid"], 500, 2200, {
                            margin: 1,
                            width: 200
                        })
                    }
                )
            break;
        }
    }
    if (find_flag == 0) {
        alert("没有找到")
    }
}
// ===================== 查询 end

document.getElementById("search_user").addEventListener("click", search_user)
